<?php

$name = explode('_', $data['form']['name']);

// Fields to mark with an asterisk
$mark = array('required', 'error');

/*
 * Field JS/CSS Requirements
 *   Iterate over the various fields and bring in any javascript
 *   and/or CSS files required by the various fields.
 */

for ($i = 0, $imax = count($data['fieldtypes']); $i < $imax; $i++)
{
    switch ($data['fieldtypes'][$i])   
    {
        case 'RichtextField':
            $html = '<script type="text/javascript" src="/Themes/Admin/JS/TinyMCE/tiny_mce_gzip.js"></script>
<script language="javascript" type="text/javascript">  
    tinyMCE_GZ.init({
        plugins : "style,table,advimage,advlink,insertdatetime,media,contextmenu,paste,directionality,noneditable,visualchars,nonbreaking",
        themes : "advanced",
        languages : "en",
        disk_cache : true,
        debug : false
    });  
</script>
<script language="javascript" type="text/javascript"> 
 	tinyMCE.init({
		elements : "'.$data['fieldsets'][0]['fields'][0]['name'].'",    
		mode : "exact",
		theme : "advanced",
		plugins : "style,table,advimage,advlink,insertdatetime,media,contextmenu,paste,directionality,noneditable,visualchars,nonbreaking",
		theme_advanced_buttons1 : "visualaid,code,|,cut,copy,paste,pastetext,pasteword,removeformat,|,undo,redo,|,link,unlink,anchor,cleanup,|,image,media,table,charmap,|,insertdate,inserttime",
		theme_advanced_buttons2 : "bullist,numlist,outdent,indent,|,justifyleft,justifycenter,justifyright,justifyfull,|,bold,italic,underline,strikethrough,|,formatselect",	
		theme_advanced_buttons3 : "",
		theme_advanced_buttons4 : "",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_path_location : "bottom",
		extended_valid_elements : "a[name|href|target|title|onclick],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]",
		file_browser_callback : "fileBrowserCallBack",
		button_tile_map : true,
		forced_root_block : "p",
		cleanup_on_startup : true
	});
</script>';            
            $this->_Model->Set('Page.Header.Raw', array($html));            
            break;
            
        case 'DateField':
            $this->_Model->Set('Page.Header.JS', array('Widgets/DatePicker/calendar.js'));
            $this->_Model->Set('Page.Header.JS', array('Widgets/DatePicker/calendar-setup.js'));
            $this->_Model->Set('Page.Header.JS', array('Widgets/DatePicker/lang/calendar-en.js'));
            break;
    }
}

// Form Header
$html = '<form name="'.$data['form']['name'].'" action="'.$data['form']['action'].'" method="'.$data['form']['method'].'" enctype="'.$data['form']['enctype'].'">'."\n";

/*
 * Process Form Actions
 */

if (isset($data['actions']) && (count($data['actions']) > 0))
{
    $html .= '<div id="content-header">
                <div class="side-a">
                 <img src="Images/icons/48x48-'.$name[0].'.png" width="48" height="48" /><span>'.ucfirst($name[0]).' : ['.ucwords(implode(' ', array_slice($name, 1))).']</span>
                </div>
              <div class="side-b">
              <input type="hidden" name="'.$data['form']['name'].'" value="test" />';
    
    for ($i = 0, $imax = count($data['actions']); $i < $imax; $i++)
        $html .= '<a href="#" onclick="javascript:document.'.$data['form']['name'].'.'.$data['form']['name'].'.value=\''.$data['actions'][$i]['name'].'\';document.'.$data['form']['name'].'.submit()"><img src="Images/actions/32x32-'.$data['actions'][$i]['name'].'.png" width="32" height="32" /><span>'.$data['actions'][$i]['title'].'</span></a>';        
               
    $html .= '</div></div>';
}

/*
 * Process Form Fields :-
 *   For the admin theme two different layouts are used depending whether or not
 *   the Richtext Field is used.
 */

if (in_array('RichtextField', $data['fieldtypes']))
{
    /*
     * RichtextField found in array, use the two column layout
     */
    
    $html .= '<div id="content-side-a">
                <div id="'.$data['fieldsets'][0]['fields'][0]['name'].'" style="height: 500px; width: 610px;">'.$data['fieldsets'][0]['fields'][0]['value'].'</div>
              </div>
              <div id="content-side-b">
                <div id="accordion">';
    
    for ($i = 1, $imax = count($data['fieldsets']); $i < $imax; $i++)
    {
        $html .= '<h3 class="accordion-click">'.$data['fieldsets'][$i]['title'].'</h3>';
        $html .= '<div class="accordion-content">
                    <table class="accordion-form">';
    
        foreach ($data['fieldsets'][$i]['fields'] as $field)
        {
            switch($field['type'])
            {
                case 'TextField':
                    $html .= '<tr>
                                <td width="100px" class="'.$field['mode'].'">'.$field['title'].((in_array($field['mode'], $mark))?'*':'').'</td>
                                <td><input type="text" name="'.$field['name'].'" value="'.$field['value'].'" /></td>
                              </tr>';
                    break;
                                    
                case 'TextareaField':
                    $html .= '<tr>
                                <td width="100px" class="'.$field['mode'].'">'.$field['title'].((in_array($field['mode'], $mark))?'*':'').'</td>
                                <td><textarea name="'.$field['name'].'">'.$field['value'].'</textarea></td>
                              </tr>';
                    break;    
                    
                case 'DateField':
                    $html .= '<tr>
                                <td width="100px" class="'.$field['mode'].'">'.$field['title'].((in_array($field['mode'], $mark))?'*':'').'</td>
                                <td><input type="text" name="'.$field['name'].'" id="'.$field['name'].'" value="'.$field['value'].'" readonly="1" /> <img src="Images/form/calendar.png" style="cursor: pointer;" id="'.$field['name'].'_trigger" />
<script type="text/javascript">
    Calendar.setup({
        inputField     :    "'.$field['name'].'",
        ifFormat       :    "%Y-%m-%d",
        button         :    "'.$field['name'].'_trigger",
        align          :    "Bl",
        singleClick    :    true
    });
</script></td>
                              </tr>';                    
                    break;
                    
                case 'SelectField':
                    
                    $options = '<option value=""> - '.$field['title'].' - </option>';
                    
                    for ($x = 0, $xmax = count($field['data']); $x < $imax; $x++)
                        $options .= '<option value="'.$field['data'][$x]['value'].'" '.(($field['data'][$x]['selected'] == 'yes')?'selected="selected"':'').'>'.$field['data'][$x]['label'].'</option>';
                    
                    $html .= '<tr>
                                <td width="100px" class="'.$field['mode'].'">'.$field['title'].((in_array($field['mode'], $mark))?'*':'').'</td>
                                <td>
                                  <select name="'.$field['name'].'">
                                  '.$options.'
                                  </select>
                                </td>
                              </tr>';   
                    
                    break;
            }
        }
    
        $html .= '</table></div>';
    }
        
    $html .= '</div></div>';
}
else 
{
    /*
     * No rich text field use the single column form layout.
     */

    for ($i = 0, $imax = count($data['fieldsets']); $i < $imax; $i++)
    {
        $html .= '<fieldset><legend>'.$data['fieldsets'][$i]['title'].'</legend>';

        foreach ($data['fieldsets'][$i]['fields'] as $field)
        {
            switch($field['type'])
            {
                case 'TextField':
                    $html .= '<div class="'.$field['mode'].'">';
                    $html .= '<label for="'.$field['name'].'">'.$field['title'].'</label>';
                    $html .= '<input type="text" name="'.$field['name'].'" value="'.$field['value'].'" />';
                    $html .= '</div>';      
                    break;
                    
                case 'TextareaField':                  
                    $html .= '<div class="'.$field['mode'].'">';
                    $html .= '<label for="'.$field['name'].'">'.$field['title'].'</label>';
                    $html .= '<textarea" name="'.$field['name'].'">'.$field['value'].'</textarea>';
                    $html .= '</div>';                 
                    break;
                    
                case 'PasswordField':
                    $html .= '<div class="'.$field['mode'].'">';
                    $html .= '<label for="'.$field['name'].'">'.$field['title'].'</label>';
                    $html .= '<input type="password" name="'.$field['name'].'" value="'.$field['value'].'" />';
                    $html .= '</div>';      
                    break;                    
            }
        }
        
        $html .= '</fieldset>';
    }
    
    /*
     * Are any actions set ?
     */

    if (!isset($data['actions']) || (count($data['actions']) == 0))
    {
        $html .= '<fieldset>
                    <div id="fm-submit" class="fm-req">
                      <div>
                        <input type="submit" name="'.$data['form']['name'].'" value="Submit" class="ButtonField"/>
                        <input type="reset" name="reset" value="Reset" class="ButtonField"/>
                      </div>
                    </div>
                  </fieldset>';
    }
}

$html .= '</form>';

return $html;

?>